package com.contactbook.web.server.dto;

import com.contactbook.web.client.dto.ContactDTO;
import com.contactbook.web.client.dto.MaritalStatusDTO;

import contactbook.model.Contact;
import contactbook.model.MaritalStatus;

public class ContactDTOConverter {
	
	public static ContactDTO convert(Contact contact, boolean referenced) {
		ContactDTO contactDTO = new ContactDTO(contact.getFirstName(), contact.getLastName());
		contactDTO.setId(contact.getId());
		contactDTO.setEmail(contact.getEmail());
		contactDTO.setAddress(contact.getAddress());
		contactDTO.setZipCode(contact.getZipCode());
		contactDTO.setTown(contact.getTown());
		contactDTO.setCountry(contact.getCountry());
		
		if (contact.getMaritalStatus() != null) {
			
			MaritalStatus maritalStatus = contact.getMaritalStatus();
			
			switch (maritalStatus) {
			case ANNULLED:
				contactDTO.setMaritalStatus(MaritalStatusDTO.ANNULLED);
				break;
			case COHABITATING:
				contactDTO.setMaritalStatus(MaritalStatusDTO.COHABITATING);
				break;
			case DIVORCED:
				contactDTO.setMaritalStatus(MaritalStatusDTO.DIVORCED);
				break;
			case ENGAGED:
				contactDTO.setMaritalStatus(MaritalStatusDTO.ENGAGED);
				break;
			case MARRIED:
				contactDTO.setMaritalStatus(MaritalStatusDTO.MARRIED);
				break;
			case SEPARATED:
				contactDTO.setMaritalStatus(MaritalStatusDTO.SEPARATED);
				break;
			case SINGLE:
				contactDTO.setMaritalStatus(MaritalStatusDTO.SINGLE);
				break;
			case WIDOWED:
				contactDTO.setMaritalStatus(MaritalStatusDTO.WIDOWED);
				break;
			default:
				break;
			}
		}
		
		contactDTO.setAge(contact.getAge());
		
		if (referenced == true) {
			contactDTO.setAccount(AccountDTOConverter.convert(contact.getAccount(), false));
			if (contact.getGroup() != null)
				contactDTO.setGroup(GroupDTOConverter.convert(contact.getGroup(), false));
		}
		
		return contactDTO;
	}
	
	public static Contact convertDTO(ContactDTO contactDTO, boolean referenced) {
		Contact contact = new Contact(contactDTO.getFirstName(), contactDTO.getLastName());
		contact.setId(contactDTO.getId());
		contact.setEmail(contactDTO.getEmail());
		contact.setAddress(contactDTO.getAddress());
		contact.setZipCode(contactDTO.getZipCode());
		contact.setTown(contactDTO.getTown());
		contact.setCountry(contactDTO.getCountry());
	
		if (contactDTO.getMaritalStatus() != null) {
			
			MaritalStatusDTO maritalStatus = contactDTO.getMaritalStatus();
	
			switch (maritalStatus) {
			case ANNULLED:
				contact.setMaritalStatus(MaritalStatus.ANNULLED);
				break;
			case COHABITATING:
				contact.setMaritalStatus(MaritalStatus.COHABITATING);
				break;
			case DIVORCED:
				contact.setMaritalStatus(MaritalStatus.DIVORCED);
				break;
			case ENGAGED:
				contact.setMaritalStatus(MaritalStatus.ENGAGED);
				break;
			case MARRIED:
				contact.setMaritalStatus(MaritalStatus.MARRIED);
				break;
			case SEPARATED:
				contact.setMaritalStatus(MaritalStatus.SEPARATED);
				break;
			case SINGLE:
				contact.setMaritalStatus(MaritalStatus.SINGLE);
				break;
			case WIDOWED:
				contact.setMaritalStatus(MaritalStatus.WIDOWED);
				break;
			default:
				break;
			}
		}
		
		contact.setAge(contactDTO.getAge());
		
		if (referenced == true) {
			contact.setAccount(AccountDTOConverter.convertDTO(contactDTO.getAccount(), false));
			if (contactDTO.getGroup() != null)
				contact.setGroup(GroupDTOConverter.convertDTO(contactDTO.getGroup(), false));
		}
		
		return contact;
	}
}
